package cc.linker.steplink.crm.task;

import cc.linker.steplink.crm.commons.lock.RedisLock;
import cc.linker.steplink.crm.merchant.service.acs.XbMerchantService;
import cc.linker.steplink.crm.merchanth5.dao.MallDao;
import cc.linker.steplink.crm.merchanth5.dao.acs.WithdrawTaskDao;
import cc.linker.steplink.crm.merchanth5.pojo.WithdrawTaskPo;
import cc.linker.steplink.crm.merchanth5.service.acs.MerchantWalletService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: CKQ
 * @DateTime: 2020/7/03 11:20
 * @Description: TODO
 */
@Slf4j
@Component
@Async
public class GoodsCountConsume {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private MallDao mallDao;

    @Scheduled(initialDelay = 10000, fixedDelay = 500)
    public void consumeGoodsCount() {
        Long goodsCount = stringRedisTemplate.opsForList().size("decrbyGoodsCountList");
        if (null != goodsCount && goodsCount > 0) {
            for (int i = 0; i < goodsCount; i++) {
                String goodsCountStr = stringRedisTemplate.opsForList().rightPop("decrbyGoodsCountList");
                try {
                    if (!StringUtils.isEmpty(goodsCountStr)) {
                        Map<String, Object> goodsCountParam = (Map) JSON.parse(goodsCountStr);
                        LOGGER.info("consumeGoodsCount---------------------goodsUpdateList:" + JSON.toJSONString(goodsCountParam));
                        mallDao.updateGoodsAllCount(goodsCountParam);
                    }
                } catch (Exception ex) {
                    LOGGER.error("consumeGoodsCount------error" + ex.getStackTrace().toString());
                    stringRedisTemplate.opsForList().rightPush("decrbyGoodsCountListError", goodsCountStr);
                }
            }
        }
    }

    @Scheduled(initialDelay = 10000, fixedDelay = 500)
    public void consumeGoodsSkuCount() {
        Long goodsSkuCount = stringRedisTemplate.opsForList().size("decrbySkuCountList");
        if (null != goodsSkuCount && goodsSkuCount > 0) {
            for (int i = 0; i < goodsSkuCount; i++) {
                String decrbySkuCountList = stringRedisTemplate.opsForList().rightPop("decrbySkuCountList");
                try {
                    if (!StringUtils.isEmpty(decrbySkuCountList)) {
                        Map<String, Object> skuCountParam = (Map) JSON.parse(decrbySkuCountList);
                        LOGGER.info("consumeGoodsCount---------------------goodsUpdateList:" + JSON.toJSONString(decrbySkuCountList));
                        mallDao.updateSkuAllCount(skuCountParam);
                    }
                } catch (Exception ex) {
                    LOGGER.error("consumeGoodsSkuCount------error" + ex.getStackTrace().toString());
                    stringRedisTemplate.opsForList().rightPush("decrbySkuCountListError", decrbySkuCountList);
                }
            }
        }
    }
}
